package com.jojo.android.zxlib.converter;

import com.alibaba.fastjson.JSONReader;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * 创建者 zx
 * 创建时间 2021/3/16 15:04
 * 转化T对象为ResponseBody对象
 */
public class FastJsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private Type type;

    public FastJsonResponseBodyConverter(Type type) {
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        JSONReader jsonReader = new JSONReader(value.charStream());
        try {
            return jsonReader.readObject(type);
        } finally {
            value.close();
        }
    }
}
